package com.trace.app.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trace.app.entity.Product;
import com.trace.app.entity.ResponseResult;
import com.trace.app.entity.User;
import com.trace.app.entity.vo.PageVo;
import com.trace.app.mapper.ProductMapper;
import com.trace.app.service.ProductService;
import com.trace.app.service.RecordService;
import com.trace.app.util.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (Product)表服务实现类
 *
 * @author lionkliu
 * @since 2024-03-17 15:55:10
 */
@Service("productService")
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    @Autowired
    private RecordService recordService;
    
    @Override
    public ResponseResult selectPage(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        Page<Product> page = new Page<>(pageNum, pageSize);
        Page<Product> productPage = page(page, queryWrapper);
        PageVo<Product> pageVo = new PageVo<>();
        pageVo.setResult(productPage.getRecords());
        pageVo.setTotal(productPage.getTotal());
        pageVo.setPageTotal(pageVo.getPageTotal(pageSize));
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult selectOne(Integer id) {
        Product product = getById(id);
        return ResponseResult.okResult(product);
    }

    @Override
    public ResponseResult insertOne(Product product) {
        return save(product) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        return removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @Override
    public ResponseResult updateOne(Product product) {
        UpdateWrapper<Product> updateWrapper = new UpdateWrapper();
        // TODO：
        return update(updateWrapper) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_ERROR);
    }

    @Override
    public ResponseResult listProduct() {
        List<Product> list = list();
        return ResponseResult.okResult(list);
    }

}

